package com.example.app;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;


import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Environment;
import android.os.IBinder;

import android.os.RemoteException;

import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import android.os.IBinder;
import android.os.IMyService;
import android.os.RemoteException;
import android.os.ServiceManager;

/**
 * Created by lianghaiming on 16-1-21.
 */
public class SingService extends Service {


    private final String mnjr = Environment.getRootDirectory() + "/data/" + "znjy-medias/mnjr.mp3";
    private final String libai = Environment.getRootDirectory() + "/data/" + "znjy-medias/lrh.mp3";
    private final String lianqu = Environment.getRootDirectory() + "/data/" + "znjy-medias/lianqu.mp3";
    private final String ybqs = Environment.getRootDirectory() + "/data/" + "znjy-medias/1874.mp3";

    public static final String BROADCAST_MUSIC_CONTROL = "SingService.ACTION.CONTROL";

    public static final int COMMAND_PLAY = 1;
    public static final int COMMAND_PAUSE = 2;
    public static final int COMMAND_NEXT = 3;
    public static final int COMMAND_PREVIOUS = 4;
    public static final int COMMAND_LED1 = 5;
    public static final int COMMAND_LED2 = 6;
    public static final int COMMAND_LED3 = 7;


    private MediaPlayer mediaPlayer = new MediaPlayer();
    private CommandReceive receive;
    private static ArrayList<String> musicArray;
    private static int number = 0;
    private static boolean IS_PAUSE = true;
    private static boolean IS_FIRST = true;
    private static int one = 0;
    private static int twe = 0;
    private static int three = 0;

    private IMyService led = null;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        bindCommandReceive();
        initMusicList();

        led = IMyService.Stub.asInterface(ServiceManager.getService("MyService"));
        try {
            led.init();
        }catch (RemoteException e) {
            e.printStackTrace();
        }


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return super.onStartCommand(intent, flags, startId);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receive);
    }


    private void bindCommandReceive() {
        receive = new CommandReceive();
        IntentFilter  filter = new IntentFilter();
        filter.addAction(SingService.BROADCAST_MUSIC_CONTROL);
        registerReceiver(receive, filter);
    }

    private void initMusicList() {

        musicArray = new ArrayList<String>();
        musicArray.add(mnjr);
        musicArray.add(libai);
        musicArray.add(lianqu);
        musicArray.add(ybqs);

    }

    class CommandReceive extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            int command = intent.getIntExtra("command", -1);
            switch (command) {
                case COMMAND_PLAY:
                    if(IS_FIRST) {
                        play(0);

                        IS_FIRST = false;

                    } else {
                    if(IS_PAUSE) {

                        resume();

                    } else {
                        pause();
                    }
                    }
                    break;
                case COMMAND_PAUSE:
                    if(IS_FIRST) {
                        play(0);
                        IS_FIRST = false;

                    }else {
                    if(IS_PAUSE) {
                        resume();
                    }else {
                        pause();
                    }
                    }
                    break;
                case COMMAND_NEXT:
                    next();
                    break;
                case COMMAND_PREVIOUS:
                    previous();
                    break;
                case COMMAND_LED1:
                    led1();
                    break;
                case COMMAND_LED2:
                    led2();
                    break;
                case COMMAND_LED3:
                    led3();
                    break;

                default:
                    break;

            }

        }
    }

    public void play(int number) {
        if(mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
        }
        load(number);
        mediaPlayer.start();
        IS_PAUSE = false;
    }

    public void resume() {
        mediaPlayer.start();
        IS_PAUSE = false;
    }

    private void pause() {
        if(mediaPlayer.isPlaying())
        {
        mediaPlayer.pause();
            IS_PAUSE = true;

        }
    }

    private void next() {
        number ++;
        if(number > musicArray.size()-1)
            number = 0;
        play(number);
        IS_PAUSE = false;
    }

    private void previous() {
        number --;
        if(number < 0)
            number = musicArray.size() -1;
        play(number);
        IS_PAUSE = false;
    }

    private void load(int number) {
        try {
            mediaPlayer.reset();
            mediaPlayer.setDataSource(musicArray.get(number));
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

        mediaPlayer.setOnCompletionListener(onCompletionListener);

    }

    MediaPlayer.OnCompletionListener onCompletionListener = new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mediaPlayer) {
            if( !mediaPlayer.isLooping()) {
                next();
            }
        }
    };

    void led1_R() {
        led.setLed(0);
        led.setLed(10);
        led.setLed(11);
    }

    void led1_B() {
        led.setLed(1);
        led.setLed(9);
        led.setLed(11);
    }

    void led1_G() {
        led.setLed(2);
        led.setLed(10);
        led.setLed(9);
    }

    void led1_C() {
        led.setLed(9);
        led.setLed(10);
        led.setLed(11);
    }

    void led2_B() {
        led.setLed(4);
        led.setLed(12);
        led.setLed(14);
    }

    void led2_R() {
        led.setLed(3);
        led.setLed(13);
        led.setLed(14);
    }

    void led2_G() {
        led.setLed(5);
        led.setLed(12);
        led.setLed(13);
    }

    void led2_C() {
        led.setLed(12);
        led.setLed(13);
        led.setLed(14);
    }

    void led3_R() {
        led.setLed(6);
        led.setLed(16);
        led.setLed(17);
    }

    void led3_B() {
        led.setLed(8);
        led.setLed(15);
        led.setLed(17);
    }

    void led3_G() {
        led.setLed(8);
        led.setLed(15);
        led.setLed(16);
    }

    void led3_C() {
        led.setLed(15);
        led.setLed(16);
        led.setLed(17);
    }

    void led1(){
        one ++;
        if(one == 1) {
            led1_G();
        } else if(one == 2) {
            led1_R();
        } else if(one == 3) {
            led1_B();
        } else if(one == 4) {
            led1_C();
        } else {
            one = 0;
        }
    }

    void led2(){
        twe ++;
        if(twe == 1) {
            led2_G();
        } else if(twe == 2) {
            led2_R();
        } else if(twe == 3) {
            led2_B();
        } else if(twe == 4) {
            led2_C();
        } else {
            twe = 0;
        }
    }

    void led3(){
        three ++;
        if(three == 1) {
            led3_G();
        } else if(three == 2) {
            led3_R();
        } else if(three == 3) {
            led3_B();
        } else if(three == 4) {
            led3_C();
        } else {
            three = 0;
        }
    }




}
